<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      setTimeout(() => {
        console.log(1)
        setTimeout(() => {
          console.log(2)
          setTimeout(() => {
            console.log(3)
          }, 3000)
        }, 2000)
      }, 1000)

      // 与以上代码效果相同，但是解决了回调地域(回调函数层层嵌套)的问题
      // promise的链式调用
      const p = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve('1')
        }, 1000)
      })
      p.then((res) => {
        console.log(res) // 1
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            resolve('2')
          }, 2000)
        })
      })
        .then((res) => {
          console.log(res) // 2
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              resolve('3')
            }, 3000)
          })
        })
        .then((res) => {
          console.log(res) // 3
        })
    </script>
  </body>
</html>
